package 剑指Offer.medium;

import java.util.HashMap;
import java.util.Map;

public class 最长不含重复字符的子字符串 {
    class Solution {
        public int lengthOfLongestSubstring(String s) {
            Map<Character, Integer> map = new HashMap<>();
            int maxLength = 0, index = -1;
            char[] charArray = s.toCharArray();
            for (int i = 0; i < charArray.length; i++) {
                if (map.containsKey(charArray[i])) {
                    index = Math.max(index, map.get(charArray[i]));
                }
                map.put(charArray[i], i);
                maxLength = Math.max(maxLength, i - index);
            }
            return maxLength;
        }
    }
}
